//----------------------------------Include-------------------------------------
#include "mcu.h"
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
//------------------------------------------------------------------------------

//--------------------------------Static Var------------------------------------
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
void mcu_int_disable(void)      { __disable_irq(); }
void mcu_int_enable(void)       { __enable_irq(); }
void mcu_reset(void)            { __set_FAULTMASK(1); NVIC_SystemReset(); }
void mcu_nop(void)              { __NOP(); }
void mcu_pri_grp_cfg(void)      { HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); }
//------------------------------------------------------------------------------

//对于 H7 而已，这两个函数要优先执行，因为 Flash 速度， SRAM 速度跟 CPU 和 TCM 有些差距的，所以要使能 Cache
//而不同存储区的 Cache 策略要通过 MPU 分别进行配置
void mcu_mpu_config(void)
{
  HAL_MPU_Disable();    //禁止MPU
  
  MPU_Region_InitTypeDef    MPU_InitStruct;
  
  //配置AXI SRAM的MPU属性为Write back, Read allocate，Write allocate
  MPU_InitStruct.Enable           = MPU_REGION_ENABLE;
  MPU_InitStruct.BaseAddress      = 0x24000000;
  MPU_InitStruct.Size             = MPU_REGION_SIZE_512KB;
  MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  MPU_InitStruct.IsBufferable     = MPU_ACCESS_BUFFERABLE;
  MPU_InitStruct.IsCacheable      = MPU_ACCESS_CACHEABLE;
  MPU_InitStruct.IsShareable      = MPU_ACCESS_NOT_SHAREABLE;
  MPU_InitStruct.Number           = MPU_REGION_NUMBER0;
  MPU_InitStruct.TypeExtField     = MPU_TEX_LEVEL1;
  MPU_InitStruct.SubRegionDisable = 0x00;
  MPU_InitStruct.DisableExec      = MPU_INSTRUCTION_ACCESS_ENABLE;
  HAL_MPU_ConfigRegion(&MPU_InitStruct);
  
  //配置FMC扩展IO的MPU属性为Device或者Strongly Ordered
  MPU_InitStruct.Enable           = MPU_REGION_ENABLE;
  MPU_InitStruct.BaseAddress      = 0x60000000;
  MPU_InitStruct.Size             = ARM_MPU_REGION_SIZE_64KB;
  MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  MPU_InitStruct.IsBufferable     = MPU_ACCESS_BUFFERABLE;
  MPU_InitStruct.IsCacheable      = MPU_ACCESS_NOT_CACHEABLE;  //不能用MPU_ACCESS_CACHEABLE;会出现2次CS、WE信号
  MPU_InitStruct.IsShareable      = MPU_ACCESS_NOT_SHAREABLE;
  MPU_InitStruct.Number           = MPU_REGION_NUMBER1;
  MPU_InitStruct.TypeExtField     = MPU_TEX_LEVEL0;
  MPU_InitStruct.SubRegionDisable = 0x00;
  MPU_InitStruct.DisableExec      = MPU_INSTRUCTION_ACCESS_ENABLE;
  HAL_MPU_ConfigRegion(&MPU_InitStruct);

  //配置SRAM4的属性为Write through, read allocate，no write allocate
  MPU_InitStruct.Enable           = MPU_REGION_ENABLE;
  MPU_InitStruct.BaseAddress      = 0x38000000;
  MPU_InitStruct.Size             = ARM_MPU_REGION_SIZE_64KB;	
  MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  MPU_InitStruct.IsBufferable     = MPU_ACCESS_NOT_BUFFERABLE;
  MPU_InitStruct.IsCacheable      = MPU_ACCESS_CACHEABLE;
  MPU_InitStruct.IsShareable      = MPU_ACCESS_NOT_SHAREABLE;
  MPU_InitStruct.Number           = MPU_REGION_NUMBER2;
  MPU_InitStruct.TypeExtField     = MPU_TEX_LEVEL0;
  MPU_InitStruct.SubRegionDisable = 0x00;
  MPU_InitStruct.DisableExec      = MPU_INSTRUCTION_ACCESS_ENABLE;
  HAL_MPU_ConfigRegion(&MPU_InitStruct);

  HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);     //使能MPU
}

void mcu_cache_enable(void)
{
  SCB_EnableICache();     // 使能 I-Cache
  SCB_EnableDCache();     // 使能 D-Cache
}
//------------------------------------EOF---------------------------------------





